// pages/manage/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialog:{
      title:"添加分类",
      display:"none",// none or flex
      value: ""
    },
    list: [{
      id: 1,
      name: "我是帅哥",
      productCount: 11
    }, {
      id: 2,
      name: "我是超级帅哥",
      productCount: 20
    }, {
      id: 3,
      name: "我是超级大帅哥",
      productCount: 25
    }, {
      id: 4,
      name: "我是神级帅哥",
      productCount: 100
    }]
  },
  dialogSave:function(){
    this.setData({
      dialog: {
        display: "none"
      }
    });
  },
  dialogCancel:function(){
    this.setData({
      dialog:{
        title: "添加分类",
        display:"none",
        value:""
      }
    });
  },
  add: function (event) {
    this.setData({
      dialog:{
        display:"flex",
        value:""
      }
    });
  },
  edit: function (event) {
    this.setData({
      dialog: {
        title: "修改分类",
        display: "flex",
        value: "test"
      }
    });
  },
  up: function (event) {
    var dataset = event.target.dataset;
    var id = dataset.id;
    if (dataset.index == 0) {
      return;
    }
    var uid = this.data.list[dataset.index - 1].id;
    this.data.list.sort(function (x, y) {
      if (x.id == uid && y.id == id) {
        return 1;
      }
      return 0;
    });
    this.setData({
      list: this.data.list
    });
  },

  down: function (event) {
    var dataset = event.target.dataset;
    if (dataset.index == this.data.list.length - 1) {
      return;
    }
    var id = dataset.id;
    var uid = this.data.list[dataset.index + 1].id;
    this.data.list.sort(function (x, y) {
      if (x.id == id && y.id == uid) {
        return 1;
      }
      return 0;
    });
    this.setData({
      list: this.data.list
    });
  },

  del: function (event) {
    var dataset = event.target.dataset;
    var list = this.data.list;
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == dataset.id) {
        list.splice(i, 1);
        break;
      }
    };
    this.setData({
      list: list
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
